// pages/goods_list/goods_list.js
import { request } from "../../request/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "精选",
        isActive: true
      },
      {
        id: 1,
        value: "销量",
        isActive: false
      },
      {
        id: 2,
        value: "价格",
        isActive: false
      },
    ],
    goodsList: []
  },
  totalPages:1,
  queryParams: {
    query: "",
    cid: null,
    pagenum: 1,
    pagesize: 10
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryParams.cid = options.cid;
    this.getGoodsList()
  },
  getGoodsList() {
    request({ url: "/goods/search", data: this.queryParams }).then(res => {
      const { goods } = res.data.message;
      const {total} = res.data.message;
      this.totalPages = Math.ceil(total / this.queryParams.pagesize);
      this.setData({
        goodsList: [...this.data.goodsList,...goods]
      })
    })
  },
  tabsItemClick(i) {
    const { index } = i.detail;
    const { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },
  onPullDownRefresh(){
  //  重置数组
  this.setData({
    goodsList:[]
  })
  this.queryParams.pagenum = 1;
  this.getGoodsList();
  // 关闭下拉刷新
  wx.stopPullDownRefresh();
  },
  onReachBottom(){
    if(this.queryParams.pagenum >= this.totalPages){
      wx.showToast({
        title: '暂无数据',
        icon:'none'
      })
    }else{
    this.queryParams.pagenum++;
    this.getGoodsList();
    }
  }
})